package short_semester.questionnaire_planet.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import short_semester.questionnaire_planet.pojo.SingleChoiceQuestion;
import short_semester.questionnaire_planet.request.fillRequest.relation.RelationRequest;
import short_semester.questionnaire_planet.response.CommonResponse;
import short_semester.questionnaire_planet.service.editService.*;

@RestController
@RequestMapping("api/relation")
public class RelationController {
    
    @Autowired
    SingleChoiceQuestionService singleChoiceQuestionService;
    @Autowired
    MultiChoiceQuestionService multiChoiceQuestionService;
    @Autowired
    FillBlankQuestionService fillBlankQuestionService;
    @Autowired
    ScoreQuestionService scoreQuestionService;
    
    @Autowired
    RelationService relationService;
    
    
    //获取题目关联
    @PostMapping("get")
    public CommonResponse getRelation(@RequestParam Integer questionId,@RequestParam String type){

        CommonResponse response = new CommonResponse();
        try {
            
            switch (type){
                case "radio":
                    response.setData(singleChoiceQuestionService.getRelation(questionId));
                    break;
                case "checkbox":
                    response.setData(multiChoiceQuestionService.getRelation(questionId));
                    break;
                case "completion":
                    response.setData(fillBlankQuestionService.getRelation(questionId));
                    break;
                case "score":
                    response.setData(scoreQuestionService.getRelation(questionId));
                    break; 
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return response;
    }
    
    @PostMapping("submit")
    public CommonResponse submitRelation(@RequestBody RelationRequest request){
        CommonResponse response = new CommonResponse();
//        System.out.println(request);
        try {
            
            relationService.submitRelation(request);

        }catch (Exception e){
            e.printStackTrace();
            response.error(e.getMessage());
        }
        
        return response;
    }
    
    @PostMapping("delete")
    public CommonResponse deleteRelation(@RequestParam Integer questionnaireId){
        CommonResponse response = new CommonResponse();
        
        try {
            relationService.delete(questionnaireId);
        }catch (Exception e){
            e.printStackTrace();
            response.error(e.getMessage());
        }
        
        return response;
    }
    
    @PostMapping("string")
    public CommonResponse relationString(@RequestParam Integer questionId,
                                         @RequestParam String type,
                                         @RequestParam String relationString){
        CommonResponse response = new CommonResponse();
        try{
            relationService.relationString(questionId,type,relationString);
            response.setData(relationString);
        }catch (Exception e){
            e.printStackTrace();
            response.error(e.getMessage());
        }
        
        return response;
    }
}
